home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter3 / isohex3_2 / isohex3_2.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-17  |  6.0 KB  |  241 lines

  1. /*****************************************************************************
  2. IsoHex3_2.cpp
  3. Ernest S. Pazera
  4. 16MAY2000
  5. Start a WIN32 Application Workspace, add in this file
  6. No other libs are required
  7. Paganini.ttf has to be in the same folder as the workspace or executable
  8. *****************************************************************************/
  9.  
  10. //////////////////////////////////////////////////////////////////////////////
  11. //INCLUDES
  12. //////////////////////////////////////////////////////////////////////////////
  13. #define WIN32_LEAN_AND_MEAN  
  14.  
  15. #include <windows.h>   
  16.  
  17. //////////////////////////////////////////////////////////////////////////////
  18. //DEFINES
  19. //////////////////////////////////////////////////////////////////////////////
  20. //name for our window class
  21. #define WINDOWCLASS "ISOHEX3"
  22. //title of the application
  23. #define WINDOWTITLE "IsoHex 3-2"
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //PROTOTYPES
  27. //////////////////////////////////////////////////////////////////////////////
  28. bool Prog_Init();//game data initalizer
  29. void Prog_Loop();//main game loop
  30. void Prog_Done();//game clean up
  31.  
  32. //////////////////////////////////////////////////////////////////////////////
  33. //GLOBALS
  34. //////////////////////////////////////////////////////////////////////////////
  35. HINSTANCE hInstMain=NULL;//main application handle
  36. HWND hWndMain=NULL;//handle to our main window
  37. HFONT hfntNew=NULL;//paganini font
  38. HFONT hfntOld=NULL;//store old font
  39.  
  40. //////////////////////////////////////////////////////////////////////////////
  41. //WINDOWPROC
  42. //////////////////////////////////////////////////////////////////////////////
  43. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  44. {
  45.     //which message did we get?
  46.     switch(uMsg)
  47.     {
  48.     case WM_DESTROY://the window is being destroyed
  49.         {
  50.  
  51.             //tell the application we are quitting
  52.             PostQuitMessage(0);
  53.  
  54.             //handled message, so return 0
  55.             return(0);
  56.  
  57.         }break;
  58.     case WM_PAINT://the window needs repainting
  59.         {
  60.             //a variable needed for painting information
  61.             PAINTSTRUCT ps;
  62.             
  63.             //start painting
  64.             HDC hdc=BeginPaint(hwnd,&ps);
  65.  
  66.             /////////////////////////////
  67.             //painting code would go here
  68.             /////////////////////////////
  69.  
  70.             //end painting
  71.             EndPaint(hwnd,&ps);
  72.                         
  73.             //handled message, so return 0
  74.             return(0);
  75.         }break;
  76.     }
  77.  
  78.     //pass along any other message to default message handler
  79.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  80. }
  81.  
  82.  
  83. //////////////////////////////////////////////////////////////////////////////
  84. //WINMAIN
  85. //////////////////////////////////////////////////////////////////////////////
  86. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  87. {
  88.     //assign instance to global variable
  89.     hInstMain=hInstance;
  90.  
  91.     //create window class
  92.     WNDCLASSEX wcx;
  93.  
  94.     //set the size of the structure
  95.     wcx.cbSize=sizeof(WNDCLASSEX);
  96.  
  97.     //class style
  98.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  99.  
  100.     //window procedure
  101.     wcx.lpfnWndProc=TheWindowProc;
  102.  
  103.     //class extra
  104.     wcx.cbClsExtra=0;
  105.  
  106.     //window extra
  107.     wcx.cbWndExtra=0;
  108.  
  109.     //application handle
  110.     wcx.hInstance=hInstMain;
  111.  
  112.     //icon
  113.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  114.  
  115.     //cursor
  116.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  117.  
  118.     //background color
  119.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  120.  
  121.     //menu
  122.     wcx.lpszMenuName=NULL;
  123.  
  124.     //class name
  125.     wcx.lpszClassName=WINDOWCLASS;
  126.  
  127.     //small icon
  128.     wcx.hIconSm=NULL;
  129.  
  130.     //register the window class, return 0 if not successful
  131.     if(!RegisterClassEx(&wcx)) return(0);
  132.  
  133.     //create main window
  134.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  135.  
  136.     //error check
  137.     if(!hWndMain) return(0);
  138.  
  139.     //if program initialization failed, then return with 0
  140.     if(!Prog_Init()) return(0);
  141.  
  142.     //message structure
  143.     MSG msg;
  144.  
  145.     //message pump
  146.     for(;;)    
  147.     {
  148.         //look for a message
  149.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  150.         {
  151.             //there is a message
  152.  
  153.             //check that we arent quitting
  154.             if(msg.message==WM_QUIT) break;
  155.             
  156.             //translate message
  157.             TranslateMessage(&msg);
  158.  
  159.             //dispatch message
  160.             DispatchMessage(&msg);
  161.         }
  162.  
  163.         //run main game loop
  164.         Prog_Loop();
  165.     }
  166.     
  167.     //clean up program data
  168.     Prog_Done();
  169.  
  170.     //return the wparam from the WM_QUIT message
  171.     return(msg.wParam);
  172. }
  173.  
  174. //////////////////////////////////////////////////////////////////////////////
  175. //INITIALIZATION
  176. //////////////////////////////////////////////////////////////////////////////
  177. bool Prog_Init()
  178. {
  179.     //retrieve the client rectangle
  180.     RECT rcClient;
  181.     GetClientRect(hWndMain,&rcClient);
  182.  
  183.     //add the paganini font to the system table
  184.     AddFontResource("Paganini.ttf");
  185.  
  186.     //create a font that uses paganini
  187.     hfntNew=CreateFont(-40,0,0,0,0,0,0,0,0,0,0,0,0,"Paganini");
  188.  
  189.     //borrow dc from main window
  190.     HDC hdc=GetDC(hWndMain);
  191.  
  192.     //select new font into dc
  193.     hfntOld=(HFONT)SelectObject(hdc,hfntNew);
  194.  
  195.     //set background mode to transparent
  196.     SetBkMode(hdc,TRANSPARENT);
  197.  
  198.     //set text color to blue
  199.     SetTextColor(hdc,RGB(0,0,255));
  200.  
  201.     //write text to dc
  202.     DrawText(hdc,"Paganini",-1,&rcClient,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  203.  
  204.     //release dc to system
  205.     ReleaseDC(hWndMain,hdc);
  206.  
  207.     return(true);//return success
  208. }
  209.  
  210. //////////////////////////////////////////////////////////////////////////////
  211. //CLEANUP
  212. //////////////////////////////////////////////////////////////////////////////
  213. void Prog_Done()
  214. {
  215.     //borrow main window dc
  216.     HDC hdc=GetDC(hWndMain);
  217.  
  218.     //restore original font
  219.     SelectObject(hdc,hfntOld);
  220.  
  221.     //return dc to system
  222.     ReleaseDC(hWndMain,hdc);
  223.  
  224.     //get rid of the font
  225.     DeleteObject(hfntNew);
  226.     
  227.     //remove the paganini font
  228.     RemoveFontResource("Paganini.ttf");
  229. }
  230.  
  231. //////////////////////////////////////////////////////////////////////////////
  232. //MAIN GAME LOOP
  233. //////////////////////////////////////////////////////////////////////////////
  234. void Prog_Loop()
  235. {
  236.     ///////////////////////////
  237.     //main game logic goes here
  238.     ///////////////////////////
  239. }
  240.  
  241.